package com.charging.common.wxpay;

import com.charging.common.constant.Constants;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

@Service
public class MyWXPayConfig extends WXPayConfig {

    private String certStr = Constants.kxSystemInfo.getWxPayCertStr();

    private String AppID = Constants.kxSystemInfo.getWxAppId();

    private String MacID = Constants.kxSystemInfo.getWxPayMacId();

    private String key = Constants.kxSystemInfo.getWxPayKey();

    private byte[] certData;
    public MyWXPayConfig()  {
        this.certData = stringToBytes(Constants.kxSystemInfo.getWxPayCertStr());
    }

    @Override
    public String getAppID() {
        return Constants.kxSystemInfo.getWxAppId();
    }

    @Override
    public String getMchID() {
        return Constants.kxSystemInfo.getWxPayMacId();
    }

    @Override
    public String getKey() {
        return Constants.kxSystemInfo.getWxPayKey();
    }

    public InputStream getCertStream() {
        ByteArrayInputStream certBis = new ByteArrayInputStream(stringToBytes(Constants.kxSystemInfo.getWxPayKey()));
        return certBis;
    }

    public int getHttpConnectTimeoutMs() {
        return 8000;
    }

    public int getHttpReadTimeoutMs() {
        return 10000;
    }
    public String getPrimaryDomain() {
        return "api.mch.weixin.qq.com";
    }

    public String getAlternateDomain() {
        return "api2.mch.weixin.qq.com";
    }

    @Override
    IWXPayDomain getWXPayDomain() {
        return WXPayDomainSimpleImpl.instance();
    }


    public static void main (String[] args) throws IOException {
//        String certPath = "D:\\temp\\msbiot\\支付宝微信\\微信支付商家\\API证书\\apiclient_cert.p12";
//        File file = new File(certPath);
//        InputStream certStream = new FileInputStream(file);
//        byte[] certData = new byte[(int) file.length()];
//        certStream.read(certData);
//        certStream.close();
//        String certStr = bytesToHexString(certData);
//        System.out.println(certStr);
//        byte[] certByte = stringToBytes(certStr);
//        if(certByte.length==certData.length){
//            for(int i =0 ;i<certData.length;i++){
//                System.out.println(certData[i]+"-"+certByte[i]);
//            }
//        }
//        for (int i =0;i<10000;i++){
//        }
    }


    /**
     * 将16进制字符串转换为byte[]
     *
     * @param str
     * @return
     */
    private static byte[] stringToBytes(String str) {
        if(str == null || str.trim().equals("")) {
            return new byte[0];
        }

        byte[] bytes = new byte[str.length() / 2];
        for(int i = 0; i < str.length() / 2; i++) {
            String subStr = str.substring(i * 2, i * 2 + 2);
            bytes[i] = (byte) Integer.parseInt(subStr, 16);
        }

        return bytes;
    }
    private static  String bytesToHexString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }
}
